package socket1;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client{
    private Socket socket;
    public Client (){
        try {
            System.out.println("正在链接客户端...");
            socket = new Socket("localhost",8089);
            System.out.println("与服务器建立连接");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void start(){
            try {
                OutputStream out = socket.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(out);
                BufferedWriter bw = new BufferedWriter(osw);
                PrintWriter pw = new PrintWriter(bw,true);
                Scanner scanner = new Scanner(System.in);
                while (true){
                    System.out.println("请输入发送到服务端的信息：");
                    String user=scanner.nextLine();
                    if("exit".equals(user)){
                        System.out.println("用户已退出！");
                        break;
                    }
                    pw.println(user);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public static void main(String [] args){
            Client client =new Client();
            client.start();
        }

}
